package com.owen.he.jee.service.impl.excel;

import java.lang.reflect.Field;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;


public class List2Excel {
   
    /**
     * 简单表头生成
     * @param ts
     * @param tClass
     * @return
     */
    public static <T> Workbook convert(List<T> ts, Class<T> tClass) {
        Workbook workbook = new SXSSFWorkbook();
        Sheet sheet = workbook.createSheet();
        /** 创建表头*/
        int i = 0;
        Row row = sheet.createRow(i++);
        Field[] fields = tClass.getDeclaredFields();
        int j = 0;
        for (Field field : fields) {
            Cell cell = row.createCell(j++);
            Column column = field.getAnnotation(Column.class);
            if (column == null) {
                cell.setCellValue(field.getName());
            } else {
                cell.setCellValue(column.value());
            }
        }
        
        /** 给excel赋值 */
        try {
            for (T t : ts) {
                row = sheet.createRow(i++);
                j = 0;
                for (Field field : fields) {

                    field.setAccessible(true);
                    Object value = field.get(t);
                    Cell cell = row.createCell(j++);

                    if (value == null) continue;

                    Class cls = field.getType();
                    if (cls == String.class) {
                        cell.setCellValue((String) value);
                    } else if (cls == Double.class || cls == double.class) {
                        cell.setCellValue((Double) value);
                    } else if (cls == Integer.class || cls == int.class) {
                        cell.setCellValue((Integer) value);
                    } else if (cls == Float.class || cls == float.class) {
                        cell.setCellValue((Short) value);
                    } else if (cls == Long.class || cls == long.class) {
                        cell.setCellValue((Long) value);
                    } else if (cls == Short.class || cls == short.class) {
                        cell.setCellValue((Short) value);
                    } else if (cls == Boolean.class || cls == boolean.class) {
                        cell.setCellValue((Boolean) value);
                    } else if (cls == Byte.class || cls == byte.class) {
                        cell.setCellValue((Byte) value);
                    } else if (cls == Date.class) {
                        cell.setCellValue((Date) value);
                    } else if (cls == Calendar.class) {
                        cell.setCellValue((Calendar) value);
                    } else {
                        cell.setCellValue(value.toString());
                    }

                }
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return workbook;
    }
    
    /**
     * 复杂表头生成
     * @param ts
     * @param tClass
     * @return
     */
    public static <T> Workbook convertDubbo(List<T> ts, Class<T> tClass) {
        Workbook workbook = new SXSSFWorkbook();
        Sheet sheet = workbook.createSheet();
        /** 创建表头*/
        int i = 0;
        Row row = sheet.createRow(i++);
        Field[] fields = tClass.getDeclaredFields();
        int j = 0;
        for (Field field : fields) {
            Cell cell = row.createCell(j++);
            Column column = field.getAnnotation(Column.class);
            if (column == null) {
                cell.setCellValue(field.getName());
            } else {
                cell.setCellValue(column.value());
            }
        }
        
        /** 给excel赋值 */
        return workbook;
    }
    
}

